package cn.oowoo.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class SentinelControllerTest {

    // cn.oowoo.sentinel.AlibabaSentinelApplication.sentinelTestRules 设置限流规则
    @RequestMapping("/sentinel/test")
    @SentinelResource(value = "sentinel.test",blockHandler = "testExceptionHandler")
    public String test(String param) {
        System.out.println(param);
        return param;
    }
    // blockHandler 函数，原方法调用被限流/降级/系统保护的时候调用
    public String testExceptionHandler(String param,BlockException ex){
        log.info(param);
        log.error("param={},资源被限流或降级!",param);
        return "资源被限流或降级testExceptionHandler";
    }
    //注意 blockHandler 函数会在原方法被限流/降级/系统保护的时候调用，而 fallback 函数会针对所有类型的异常。
    public String fallback(String param,BlockException ex){
        log.error("fallback-->param={},资源异常fallback!",param,ex);
        return "资源异常fallback";
    }

}
